<%/**
			 * leaderboard.jsp
			 * 
			 * Contains the selection for input of leaderboard
			 *
			 * @author Samson
			 */%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="b1w1nn1ng.crossf1t.db.*"%>
    <%@page import="b1w1nn1ng.crossf1t.gui.*"%>
    <%@page import="b1w1nn1ng.crossf1t.business.*"%>
    <%@page import="java.util.*"%>
    
    	<%
        	//this is a convienience variable: If it is true, then we have sufficient information to show a graph
        	boolean ready = false;
        %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	User user = (User) session.getAttribute("user");
	session.setAttribute("user", user);
	Calendar cal = Calendar.getInstance();
%>
	<head>
		<title> Leaderboard </title>
		<jsp:include page="/detectBrowser.jsp" />	
	</head>
	<body>
	<div id="container">
		<jsp:include page="/navbar.jsp" />	
		<h1> Leaderboard </h1> <hr />
		Select an Exercise type:
		<form action="leaderboard.jsp" method="get">
		<select name="primary" onchange="this.form.submit();" class="selects"> 
			<%
 				//get the different values and add them as select options
 				String[] primaries = {"Timed","Reps","Weight"};
				primaries = Database.getPrimarySeeList(true);

 				//Print the "Categories" option at the top of the menu if we haven't selected one yet
 				if (request.getParameter("primary") == null) {
 					out.println("<option>Categories</option>");
 				}

 				//for each entry in the string array, prints it as an option in format:
 				// <option value="entry">entry</option>
 				for (String entry : primaries) {

 					// If an option was selected last time, we do this check to keep it selected:
 					if (request.getParameter("primary") != null) {
 						if (request.getParameter("primary").equals(entry))
 							out.println("<option selected value=\"" + entry + "\">"
 									+ entry + "</option>");
 						else
 							out.println("<option value=\"" + entry + "\">" + entry
 									+ "</option>");
 						// Else, just print the options
 					} else {
 						out.println("<option value=\"" + entry + "\">" + entry
 								+ "</option>");
 					}
 				}
 			%>
		</select>
		<%
			// If a primary category was selected, we may have to display the secondary menu. Check
			// if we have to here:
			if (request.getParameter("primary") != null) {
				String[] secondary = Database.getSecondarySeeList(request
						.getParameter("primary"));

				//if the secondary seeList is null, we're done, there is no secondary.
				if (secondary == null) {
					ready = true;
				} else {
					//else, there is a secondary: here's the select menu for it.
					out.println("<select  class=\"selects\" name=\"secondary\" onchange=\"this.form.submit();\">");
					//Print the "Excercise" option at the top of the menu if we haven't selected one yet
					//OR if primary changed since last time.
					if (request.getParameter("secondary") == null) {
						out.println("<option>Excercise</option>");
						ready = false;
					} else {
						boolean found = false;
						//check if primary changed by seeing if selected secondary is in the list still
						for (int i = 0; i < secondary.length; i++) {
							if (secondary[i].equals(request
									.getParameter("secondary"))) {
								found = true;
								ready = true;
								break;
							}
						}
						if (!found) {
							out.println("<option>Excercise</option>");
							ready = false;
						}
					}

					//for each entry in the string array, prints it as an option in format:
					// <option value="entry">entry</option>
					for (String entry : secondary) {

						// If an option was selected last time, we do this check to keep it selected:
						if (request.getParameter("secondary") != null) {
							if (request.getParameter("secondary").equals(entry))
								out.println("<option selected value=\"" + entry
										+ "\">" + entry + "</option>");
							else
								out.println("<option value=\"" + entry + "\">"
										+ entry + "</option>");
							// Else, just print the options
						} else {
							out.println("<option value=\"" + entry + "\">"
									+ entry + "</option>");
						}
					}
					out.println("</select>");
				}
			}
		%>		
		</form><br>
		
		<%
		//leaderboard stuff
					//out.println("The leaderboard " + (ready ? "IS" : "IS NOT")
						//	+ " ready to be displayed.\n");
					if (ready) {
						//this is where we put the leaderboard goodness
						if (request.getParameter("secondary") != null) {
							out.println("The page will display the leaderboard for "
									+ request.getParameter("secondary") + "<br/>");

							String primary = request.getParameter("primary");//"Timed","Reps","Weight"
							String secondary = request.getParameter("secondary");//the type of exercise. We are ONLY comparing individual exercises.

							
							//for Timed exercises...
							if (primary.equals("Timed")) {

								//go through all the user's friends (and the user itself) to find the top 5 friends for the particular exercise
								ArrayList<Integer> friendIDs = user.getFriends();
								int[] topFiveScores = { 99999, 99999, 99999, 99999,
										99999 };//in seconds
								String[] topFiveNames = {"Nobody","Nobody","Nobody","Nobody","Nobody"};
								for (int i = 0; i < friendIDs.size(); i++) {
									User friend = Database.getUserByID(friendIDs.get(i));
									//int currentPR = Exercise.getPRExercise(friendIDs.get(i), primary, secondary);
									Random rand = new Random();
									int currentPR = rand.nextInt(60);
									while(currentPR < 10){
										currentPR = rand.nextInt(60);
									}
									if(friend!=null)
									{
										//see if the PRExercise of each friend is in the top 5 (We want low scores for Timed exercises)
										for (int j = 0; j < topFiveScores.length; j++) {
											if (currentPR < topFiveScores[j]) {
												topFiveScores[j] = currentPR;
												topFiveNames[j] = friend.getUsername();
												break;
											}
										}
									}
								}
								//now we have the top five friends for the particular exercise (topFive might all be 99999 still if noone did the exercise...)
								//sort the topFive in ascending order (using bubble sort. yes...bubble sort) 
								for (int i = 0; i < topFiveScores.length; i++) {
									for (int j = 0; j < topFiveScores.length; j++) {
										if (topFiveScores[j] > topFiveScores[i]) {
											int temp = topFiveScores[i];
											topFiveScores[i] = topFiveScores[j];
											topFiveScores[j] = temp;

											String temp2 = topFiveNames[i];
											topFiveNames[i] = topFiveNames[j];
											topFiveNames[j] = temp2;
										}
									}
								}

								//and display them
								for (int j = 0; j < topFiveNames.length; j++) {
									out.print(topFiveNames[j] + ": " + topFiveScores[j] + "<br/>");
								}

								
								
								
							} else {//goes here for "Reps" and "Weight"

								//go through all the user's friends (and the user itself) to find the top 5 friends for the particular exercise
								ArrayList<Integer> friendIDs = user.getFriends();
								int[] topFiveScores = { 0, 0, 0, 0, 0 };
								String[] topFiveNames = { "Nobody","Nobody","Nobody","Nobody","Nobody"};
								for (int i = 0; i < friendIDs.size(); i++) {
									User friend = Database
											.getUserByID(friendIDs.get(i));
									//int currentPR = Exercise.getPRExercise(
											//friendIDs.get(i), primary, secondary);
									Random rand = new Random();
									int currentPR = rand.nextInt(60);
									while(currentPR < 10){
										currentPR = rand.nextInt(60);
									}
									if(friend!=null)
									{
										//see if the PRExercise of each friend is in the top 5
										for (int j = 0; j < topFiveScores.length; j++) {
											if (currentPR > topFiveScores[j]) {
												topFiveScores[j] = currentPR;
												topFiveNames[j] = friend.getUsername();
												break;
											}
										}
									}
								}
								//now we have the top five friends for the particular exercise (topFive might all be 0 still if noone did the exercise...)
								//sort the topFive in descending order (using bubble sort. yes...bubble sort) 
								for (int i = 0; i < topFiveScores.length; i++) {
									for (int j = 0; j < topFiveScores.length; j++) {
										if (topFiveScores[j] < topFiveScores[i]) {
											int temp = topFiveScores[i];
											topFiveScores[i] = topFiveScores[j];
											topFiveScores[j] = temp;

											String temp2 = topFiveNames[i];
											topFiveNames[i] = topFiveNames[j];
											topFiveNames[j] = temp2;
										}
									}
								}

								//and display them
								for (int j = 0; j < topFiveNames.length; j++) {
									out.print(topFiveNames[j] + ": " + topFiveScores[j] + "<br/>");
								}
							}
						}

						else {
							out.println("The page will display the leaderboard for "
									+ request.getParameter("primary"));
							out.println("This shouldn't be displayed for the leaderboard...change this");
						}
					}
				%>
				</div>
		
				
	</body>
</html>


